package com.chucang.shucang.auth.support.core;

import com.chucang.shucang.common.base.constant.SecurityConstant;
import com.chucang.shucang.common.security.entity.ShuCangUser;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenClaimsContext;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenClaimsSet;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/9/16 11:18
 * @description 解析返回用户信息
 */
public class CustomOAuth2TokenCustomizer implements OAuth2TokenCustomizer<OAuth2TokenClaimsContext> {
    /**
     * Customize the OAuth 2.0 Token attributes.
     *
     * @param context the context containing the OAuth 2.0 Token attributes
     */
    @Override
    public void customize(OAuth2TokenClaimsContext context) {
        OAuth2TokenClaimsSet.Builder claims = context.getClaims();
        claims.claim(SecurityConstant.DETAILS_LICENSE, SecurityConstant.PROJECT_LICENSE);
        String clientId = context.getAuthorizationGrant().getName();
        claims.claim(SecurityConstant.CLIENT_ID, clientId);
        // 客户端模式不返回具体用户信息
        if (SecurityConstant.CLIENT_CREDENTIALS.equals(context.getAuthorizationGrantType().getValue())) {
            return;
        }
        ShuCangUser shuCangUser = (ShuCangUser) context.getPrincipal().getPrincipal();
        claims.claim(SecurityConstant.DETAILS_USER, shuCangUser);
    }
}
